package ${sourceBasePackage}.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.idp.common.constant.SysMessages;
import com.idp.common.error.SystemException;
import com.idp.common.model.PageModel;
import ${sourceBasePackage}.dto.${entityName}Dto;
import ${sourceBasePackage}.dto.${entityName}SearchDto;
import ${sourceBasePackage}.entity.${entityName}Entity;
import ${sourceBasePackage}.mapper.${entityName}Mapper;
import ${sourceBasePackage}.service.${entityName}Service;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ${description}Service实现类
 * @author ${author}
 */
@Service("${entityName?uncap_first}Service")
public class ${entityName}ServiceImpl implements ${entityName}Service {

	@Autowired
	private ${entityName}Mapper ${entityName?uncap_first}Mapper;

	@Override
	public PageModel<${entityName}Dto> findByPage(${entityName}SearchDto ${entityName?uncap_first}SearchDto) {

		PageModel<${entityName}Dto> page = new PageModel<${entityName}Dto>(${entityName?uncap_first}SearchDto.getPage(), ${entityName?uncap_first}SearchDto.getRows());

		<#list searchFields as po>
			<#if po.listQuery == 'interval' && po.propertyType == 'Date'>
		if(ObjectUtils.isNotEmpty(${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}End())) {
		${entityName?uncap_first}SearchDto.set${po.propertyName?cap_first}End(DateUtil.endOfDay(${entityName?uncap_first}SearchDto.get${po.propertyName?cap_first}End()));
		}
			</#if>
		</#list>

		Page<${entityName}Dto> list = ${entityName?uncap_first}Mapper.findByPage(${entityName?uncap_first}SearchDto);
		page.initData(list);

		return page;
	}

	@Override
	public ${entityName}Dto getById(${pkPropertyType} ${pkPropertyName}) {

		${entityName}Dto ${entityName?uncap_first}Dto = ${entityName?uncap_first}Mapper.getById(${pkPropertyName});

		if(${entityName?uncap_first}Dto == null) {
			throw new SystemException(SysMessages.DATA_NOT_EXIST.value, SysMessages.DATA_NOT_EXIST.i18n);
		}

		return ${entityName?uncap_first}Dto;
	}

	@Override
	public void add(${entityName}Dto ${entityName?uncap_first}Dto) {

		${entityName}Entity ${entityName?uncap_first}Entity = BeanUtil.toBean(${entityName?uncap_first}Dto, ${entityName}Entity.class);

		${entityName?uncap_first}Mapper.add(${entityName?uncap_first}Entity);
	}

	@Override
	public void update(${entityName}Dto ${entityName?uncap_first}Dto) {

		${entityName}Dto ${entityName?uncap_first} = ${entityName?uncap_first}Mapper.getById(${entityName?uncap_first}Dto.get${pkPropertyName?cap_first}());

        if(${entityName?uncap_first} == null) {
            throw new SystemException(SysMessages.DATA_NOT_EXIST.value, SysMessages.DATA_NOT_EXIST.i18n);
        }

		${entityName}Entity ${entityName?uncap_first}Entity = BeanUtil.toBean(${entityName?uncap_first}, ${entityName}Entity.class);
		BeanUtil.copyProperties(${entityName?uncap_first}Dto, ${entityName?uncap_first}Entity);

		${entityName?uncap_first}Mapper.update(${entityName?uncap_first}Entity);
	}

	@Override
	public void delete(${pkPropertyType} ${pkPropertyName}) {

		${entityName}Dto ${entityName?uncap_first} = ${entityName?uncap_first}Mapper.getById(${pkPropertyName});

        if(${entityName?uncap_first} == null) {
            throw new SystemException(SysMessages.DATA_NOT_EXIST.value, SysMessages.DATA_NOT_EXIST.i18n);
        }

		${entityName?uncap_first}Mapper.delete(${pkPropertyName});
	}

}
